<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class CateController extends Controller
{   
    //将 数据显示前台页面
    static public function getCatepid($pid){  //  0 
        $data = DB::table('cate')->where('pid',$pid)->get();
        $arr = array();
        foreach($data as $k=>$v){
           $v['sub'] = self::getCatepid($v['id']);
           $arr[] = $v;
        }
        return $arr;
        
    }
    static public function getCate(){
         // 查询数据 进行排序
        $data = DB::table('cate') ->select(DB::raw("*,concat(path,',',id) as paths")) -> orderBy('paths')-> get();
       
        foreach($data as $k=>$v){
        // 计算path 逗号出现的次数
          $l = substr_count($v['path'],',');
            $data[$k]['name'] = str_repeat('|----',$l).$v['name'];
        
       }
       return $data;
    }
// 分类添加页
   public function getAdd(Request $request){
       $id = $request->input('id');
       return view('admin/cate/add',['data'=>self::getCate(),'id'=>$id]);
   }
// 分类添加
   public function postInsert(Request $request){
        $arr = $request->only(['name','pid']);
        $pid = $request -> input('pid');
        // 处理数据 
        if($pid == 0){
            $arr['path'] = 0;
        }else{
            $res = DB::table('cate')->where('id',$pid)->first();
             $arr['path'] = $res['path'].','.$res['id'];
           
        }
        
        // 数据添加
        $r = DB::table('cate')-> insert($arr);
       if($r){
        return redirect('/admin/cate/index')->with('success','添加成功');
       }else{
        return back()->with('error','添加失败');
       }
    }
// 分类列表页
    public function getIndex(){
        if(empty(session('id'))){
        return redirect('/admin')->with('error','请您登陆');
      }
        // 分配数据  解析模板
        return view('/admin/cate/index',['data'=>self::getCate()]);
    }
//分类删除
public function getDelete($id){
    $res = DB::table('cate')->where('pid',$id)->first();
  
    if($res){
       return back()->with('error','当前类别有子类，不允许删除');
    }
   $data = DB::table('cate')->where('id',$id)->delete();
   if($data){
    return redirect('/admin/cate/index')->with('success','删除成功');
   }else{
    return back()->with('error','删除失败');
   }
 }   
// 分类修改
 public function getEdit($id){
    $datas = DB::table('cate')->where('id',$id)->first();

    // dd($datas);
   return view('admin/cate/edit',['data'=>self::getCate(),'datas'=>$datas]);
 }
// 分类修改
 public function postUpdate(Request $request){
     $arr = $request->only(['name','pid']); // 11 /// 2

        $pid = $request -> input('pid');
        // 处理数据 
        if($pid == 0){
            $arr['path'] = 0;
        }else{
            $res = DB::table('cate')->where('id',$pid)->first();
             $arr['path'] = $res['path'].','.$res['id'];
         } 
         
       // 数据修改
         $data = DB::table('cate')->where('id',$request->input('id'))->update($arr);
       if($data == 1){
        return redirect('/admin/cate/index')->with('success','修改成功');
       }else{
        return back()->with('error','修改失败');
       }
}
 // 

}
